package com.yuedou.api.service.sys.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yuedou.api.dao.sys.SysNoticeMapper;
import com.yuedou.api.entity.sys.SysNotice;
import com.yuedou.api.service.base.impl.BaseServiceImpl;
import com.yuedou.api.service.sys.SysNoticeService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 公告 服务层实现
 *
 * @author ruoyi
 */
@Service
public class SysNoticeServiceImpl extends BaseServiceImpl<SysNoticeMapper, SysNotice> implements SysNoticeService {

    @Resource
    private SysNoticeMapper sysNoticeMapper;
    @Resource
    protected RedisTemplate<String, Object> redisTemplate;


    @Override
    public List<SysNotice> getList(int userId) {
        QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
        qwNotice.eq("status","0");
        Integer[] types = { 1, 2};
        qwNotice.in("notice_type", Arrays.asList(types));
        qwNotice.orderByDesc("create_time");
        List<SysNotice> notices = sysNoticeMapper.selectList(qwNotice);
        return notices;
    }

    @Override
    public List<SysNotice> getListByType(int userId, int notice_type, int pageIndex, int pageSize) {
        QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
        qwNotice.eq("status","0");
        if(notice_type <= 2){
            Integer[] types = { 1, 2};
            qwNotice.in("notice_type", Arrays.asList(types));
        } else{
            qwNotice.eq("notice_type", notice_type);
        }
        qwNotice.orderByDesc("create_time");
        List<SysNotice> notices = sysNoticeMapper.getPageEntityList(qwNotice,pageIndex,pageSize);
        return notices;
    }

    @Override
    public void resetDate() {
        QueryWrapper<SysNotice> qwNotice = new QueryWrapper<>();
        qwNotice.eq("status","0");
        Integer[] types = { 1, 2};
        qwNotice.in("notice_type", Arrays.asList(types));
        String key = "sys_notice_1_2";
        List<SysNotice> notices = sysNoticeMapper.selectList(qwNotice);
        redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(notices));
    }
}
